﻿// See https://aka.ms/new-console-template for more information
using JT809.GpsRedisConsumer;
using log4net.Config;
using Microsoft.IdentityModel.Logging;
using Quartz.Impl;
using Quartz.Spi;
using Quartz;
using System.Runtime.InteropServices;
using SqlSugar.IOC;

XmlConfigurator.Configure(JT809.GpsRedisConsumer.LogHelper.Repository, new FileInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log4net.config")));

var builder = WebApplication.CreateBuilder(args);
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
    builder.Host.UseWindowsService();
}

if (bool.TryParse(builder.Configuration.GetSection("Redis:Enabled").Value, out var redisEnabled))
{
    var _redisConnection = builder.Configuration.GetSection("Redis:Connection").Value;
    RedisHelper.Initialization(new CSRedis.CSRedisClient(_redisConnection));
}

builder.Services.AddHostedService<MyQuartHostedService>();
builder.Services.AddSingleton<ISchedulerFactory, StdSchedulerFactory>();//注册ISchedulerFactory的实例。
builder.Services.AddSingleton<QuartzStartup>();
builder.Services.AddSingleton<IJobFactory, IOCJobFactory>();
builder.Services.AddTransient<JobDetail>();

//Sqlsugar
builder.Services.AddSqlSugar(new IocConfig()
{
    ConfigId = "sql",
    ConnectionString = builder.Configuration.GetConnectionString("sqlConnection"),
    DbType = IocDbType.SqlServer,
    IsAutoCloseConnection = true,//自动释放
});

builder.Services.AddSqlSugar(new IocConfig()
{
    ConfigId = "sql2",
    ConnectionString = builder.Configuration.GetConnectionString("sqlConnection2"),
    DbType = IocDbType.SqlServer,
    IsAutoCloseConnection = true,//自动释放
});

var app = builder.Build();

app.Run();